<?php
include_once('./inc/config.php');
if(!is_logged_in()) redirect('index.php');	

$mode = $_GET['mode'];
$an_code = isset($_SESSION['domain_sel']['c_code']) ? $_SESSION['domain_sel']['c_code']:'';
if(empty($an_code)) redirect('dashboard.php');

$actions = array();
switch($mode)
{
	case 'pg_visits':
		get_page_visits($an_code);
	break;
	
	case 'pg_ref':
		get_page_ref($an_code);
	break;
	
	case 'browser':
		get_browser_data($an_code);
	break;
	
	case 'srch_eng':
		get_search_engine($an_code);
	break;
	
	case 'ip_add':
		get_ip_add($an_code);
	break;
	
	case 'pgldtime':
		get_pg_ld_time($an_code);
	break;	
	
	case 'pgvisitcnt':
		get_pg_visit_counter($an_code);
	break;	
	
	case 'pgendtime':
		get_pg_end_time($an_code);
	break;
	
	case 'pgmostvisit':
		get_pg_most_visited($an_code);
	break;
	
	case 'resol':
		get_pg_resol($an_code);
	break;	
	
	default:
		redirect('dashboard.php');
 	break;	
}



/* ------------------------------------------------------------------------- */

/** 
 * Get page resolution
 *
 * @desc this will get page resolution
 * @access public
 * @param string 
 * @return void
 */
function get_pg_resol($an_code)
{
	global $dbobj, $proj_config;
	$ret = array();	
	$ret['info_cnt'] = 0;
	$ret['info'] = array();
	
	//Get total page resolution data array and the no of rows
	$query = "SELECT `sc_height`, `sc_width`, `sc_availWidth`, `sc_availHeight`
			FROM ".TBL_ANA_DATA." 
			WHERE an_code = '".$an_code."'						
			group by `sc_height`, `sc_width`, `sc_availWidth`, `sc_availHeight`
			order by `sc_height` desc, `sc_width` desc";
	// Perform Query	
	$result = $dbobj->MySQLDB_GetData($query,'ARRAY');	
	// Get num rows from the result set	
	$no_rows = $dbobj->MySQLDB_Numrows();				
	$ret['info_cnt'] = $no_rows;
	if($no_rows > 0)
	{
		$ret['info'] = $result;	
	}	
 	display_view($ret, 'an_list_pg_resol');
}

/* ------------------------------------------------------------------------- */

/** 
 * Get page most visited
 *
 * @desc this will get the total page most visited
 * @access public
 * @param string 
 * @return void
 */
function get_pg_most_visited($an_code)
{
	global $dbobj, $proj_config;
	$ret = array();	
	$ret['info_cnt'] = 0;
	$ret['info'] = array();
	
	//Get total page most visited data array and the no of rows
	$query = "SELECT location_href as url, 
				count(an_id) AS visited, 
				ip_add
				FROM ".TBL_ANA_DATA." 
				WHERE an_code = '".$an_code."'
				GROUP BY location_href, ip_add
				ORDER BY visited DESC";
	// Perform Query	
	$result = $dbobj->MySQLDB_GetData($query,'ARRAY');	
	// Get num rows from the result set	
	$no_rows = $dbobj->MySQLDB_Numrows();				
	$ret['info_cnt'] = $no_rows;
	if($no_rows > 0)
	{
		$ret['info'] = $result;	
	}	
 	display_view($ret, 'an_list_pg_most_visit');
}

/* ------------------------------------------------------------------------- */

/** 
 * Get page end time
 *
 * @desc this will get the total time on a page
 * @access public
 * @param string 
 * @return void
 */
function get_pg_end_time($an_code)
{
	global $dbobj, $proj_config;
	$ret = array();	
	$ret['info_cnt'] = 0;
	$ret['info'] = array();
	
	//Get total time on a page data array and the no of rows
	$query = "SELECT 
				location_href as url,
				an_created,
				total_time as ld_time				
				FROM ".TBL_TIME_PG."
				WHERE an_code = '".$an_code."'
				ORDER BY total_time desc, an_created desc ";
	// Perform Query	
	$result = $dbobj->MySQLDB_GetData($query,'ARRAY');	
	// Get num rows from the result set	
	$no_rows = $dbobj->MySQLDB_Numrows();				
	$ret['info_cnt'] = $no_rows;
	if($no_rows > 0)
	{
		$ret['info'] = $result;	
	}
 	display_view($ret, 'an_list_pg_end_time');
}

/* ------------------------------------------------------------------------- */

/** 
 * Get page visit counter
 *
 * @desc this will get the page vistor counter who have returned to the page more than once
 * @access public
 * @param string 
 * @return void
 */
function get_pg_visit_counter($an_code)
{
	global $dbobj, $proj_config;
	$ret = array();	
	$ret['info_cnt'] = 0;
	$ret['info'] = array();
	
	//Get total page visits data array and the no of rows
	$query = "SELECT location_protocol,
				location_host,
				location_pathname, 
				max( `page_visits` ) AS cnt
				FROM ".TBL_ANA_DATA." 
				WHERE an_code = '".$an_code."'
				GROUP BY `location_pathname`
				ORDER BY cnt DESC";			
	// Perform Query	
	$result = $dbobj->MySQLDB_GetData($query,'ARRAY');	
	// Get num rows from the result set	
	$no_rows = $dbobj->MySQLDB_Numrows();				
	$ret['info_cnt'] = $no_rows;
	if($no_rows > 0)
	{
		$ret['info'] = $result;	
	}
	
 	display_view($ret, 'an_list_pg_visit_counter');
}

/* ------------------------------------------------------------------------- */

/** 
 * Get page load time
 *
 * @desc this will get the total page load time for a page
 * @access public
 * @param string 
 * @return void
 */
function get_pg_ld_time($an_code)
{
	global $dbobj, $proj_config;
	$ret = array();	
	$ret['info_cnt'] = 0;
	$ret['info'] = array();
	
	//Get total page load time data array and the no of rows
	$query = "SELECT 
				location_href as url, 
				total_page_load_time AS ld_time, 
				an_created	
				FROM ".TBL_ANA_DATA." 
				WHERE an_code = '".$an_code."'
				AND location_href != ''
				ORDER BY total_page_load_time, an_created DESC";
	// Perform Query	
	$result = $dbobj->MySQLDB_GetData($query,'ARRAY');	
	// Get num rows from the result set	
	$no_rows = $dbobj->MySQLDB_Numrows();				
	$ret['info_cnt'] = $no_rows;
	if($no_rows > 0)
	{
		$ret['info'] = $result;	
	}
	
 	display_view($ret, 'an_list_pg_ld_time');
}

/* ------------------------------------------------------------------------- */

/** 
 * Get IP Address
 *
 * @desc this will get all IP address
 * @access public
 * @param string 
 * @return void
 */
function get_ip_add($an_code)
{
	global $dbobj, $proj_config;
	$ret = array();	
	$ret['info_cnt'] = 0;
	$ret['info'] = array();
	
	//Get IP Address data array and the no of rows
	$query = "SELECT 
				DISTINCT(ip_add)		
				FROM ".TBL_ANA_DATA." 
				WHERE an_code = '".$an_code."'
				AND ip_add != ''
				ORDER BY ip_add DESC ";
	// Perform Query	
	$result = $dbobj->MySQLDB_GetData($query,'ARRAY');	
	// Get num rows from the result set	
	$no_rows = $dbobj->MySQLDB_Numrows();				
	$ret['info_cnt'] = $no_rows;
	if($no_rows > 0)
	{
		$ret['info'] = $result;	
	}
	
 	display_view($ret, 'an_list_ip_add');
}

/* ------------------------------------------------------------------------- */

/** 
 * Get search engine
 *
 * @desc this will get search engine data with logo
 * @access public
 * @param string 
 * @return void
 */
function get_search_engine($an_code)
{
	global $dbobj, $proj_config;
	$ret = array();	
	$ret['info_cnt'] = 0;
	$ret['info'] = array();
	
	//Get search engine data array and the no of rows
	$query = "SELECT 
				DISTINCT(doc_ref) as url,
				an_created			
				FROM ".TBL_ANA_DATA." 
				WHERE an_code = '".$an_code."'
				AND doc_ref != ''
				ORDER BY doc_ref DESC ";
	// Perform Query	
	$result_set = $dbobj->MySQLDB_GetData($query,'ARRAY');	
	$result = array();
	// Get num rows from the result set	
	$no_rows = $dbobj->MySQLDB_Numrows();				
	
	if($no_rows > 0)
	{
		foreach($result_set as $key => $val)
		{
			$br_d = search_engine_check( $val['url'], true);//push the data in the same array
			if(!empty($br_d))
			{
				$result[$key]['br'] = $br_d;
				if(preg_match('/google./iu',$val['url']))
				{ 
					if(preg_match('/q=/iu',$val['url']))
					{ 
						//google keywords
						preg_match_all('/q=(.*)(.*)+/si',$val['url'],$g);
						$key_words = (isset($g[1][0]))?$g[1][0]:'';
					}
					else if(preg_match('/imgurl=/iu',$val['url']))
					{
						//google keywords
						preg_match_all('/imgurl=(.*)(.*)+/si',$val['url'],$g);
						$key_words = (isset($g[1][0]))?$g[1][0]:'';
					}					
				}
				else if(preg_match('/aol./iu',$val['url']))			
				{ 
					//aol keywords
					preg_match_all('/enabled_terms=(.*)(.*)+/si',$val['url'],$g);
					$key_words = (isset($g[1][0]))?$g[1][0]:'';
				}
				else if(preg_match('/lycos./iu',$val['url']))			
				{ 
					//lycos keywords
					preg_match_all('/query=(.*)(&)(&*?)/si',$val['url'],$g);
					$key_words = (isset($g[1][0]))?$g[1][0]:'';
				}
				else if(preg_match('/bing.com/iu',$val['url']))			
				{ 
					//bing keywords
					preg_match_all('/q=(.*)(.*)+/si',$val['url'],$g);
					$key_words = (isset($g[1][0]))?$g[1][0]:'';
				}
				else if(preg_match('/yahoo.com/iu',$val['url']))			
				{ 
					//bing keywords
					preg_match_all('/p=(.*)(.*)+/si',$val['url'],$g);
					$key_words = (isset($g[1][0]))?$g[1][0]:'';
				}
				
				$result[$key]['key_words'] = urldecode(stripslashes($key_words));
				$result[$key]['url'] = $val['url'];
				$result[$key]['an_created'] = $val['an_created'];
			}
		}
		$ret['info'] = $result;	
		$ret['info_cnt'] = count($result);
	}
	
 	display_view($ret, 'an_list_search_eng');
}


/* ------------------------------------------------------------------------- */

/** 
 * Get all browser data
 *
 * @desc this will get all the browser data with logo
 * @access public
 * @param string 
 * @return void
 */
function get_browser_data($an_code)
{
	global $dbobj, $proj_config;
	$ret = array();	
	$ret['info_cnt'] = 0;
	$ret['info'] = array();
	
	//Get page browser data array and the no of rows
	$query = "SELECT 
				DISTINCT(br_userAgent) as url,
				br_name,
				br_version,
				br_cookie_enb,
				br_platform,
				br_language,
				br_product				
				FROM ".TBL_ANA_DATA." 
				WHERE an_code = '".$an_code."'
				AND br_userAgent != ''
				ORDER BY br_name DESC,br_version ";
	
	// Perform Query	
	$result = $dbobj->MySQLDB_GetData($query,'ARRAY');	
	// Get num rows from the result set	
	$no_rows = $dbobj->MySQLDB_Numrows();				
	$ret['info_cnt'] = $no_rows;
	if($no_rows > 0)
	{
		foreach($result as $key => $val)
		{
			$result[$key]['br'] = browser_check( $val['url'], true);//push the data in the same array
		}
		$ret['info'] = $result;	
	}
	
 	display_view($ret, 'an_list_browser');
}

/* ------------------------------------------------------------------------- */

/** 
 * Get all page referrals
 *
 * @desc this will get all the link of page referrals
 * @access public
 * @param string 
 * @return void
 */
function get_page_ref($an_code)
{
	global $dbobj, $proj_config;
	$ret = array();	
	$ret['info_cnt'] = 0;
	$ret['info'] = array();
	
	//Get page referrals data array and the no of rows
	$query = "SELECT 
				DISTINCT(doc_ref) as url,
				an_created			
				FROM ".TBL_ANA_DATA." 
				WHERE an_code = '".$an_code."'
				AND doc_ref != ''
				ORDER BY an_created DESC";
	// Perform Query	
	$result = $dbobj->MySQLDB_GetData($query,'ARRAY');	
	// Get num rows from the result set	
	$no_rows = $dbobj->MySQLDB_Numrows();				
	$ret['info_cnt'] = $no_rows;
	if($no_rows > 0)
	{
		$ret['info'] = $result;	
	}
 	display_view($ret, 'an_list_pg_referral');
}

/* ------------------------------------------------------------------------- */

/** 
 * Get all page vists
 *
 * @desc this will get all the link of page vists
 * @access public
 * @param string 
 * @return void
 */
function get_page_visits($an_code)
{
	global $dbobj, $proj_config;
	$ret = array();	
	$ret['info_cnt'] = 0;
	$ret['info'] = array();
	
	//Get page visits data array and the no of rows
	$query = "SELECT 
				DISTINCT(location_href) as url,
				location_protocol,
				location_host,
				location_pathname				
				FROM ".TBL_ANA_DATA." 
				WHERE an_code = '".$an_code."'
				AND location_href != ''
				ORDER BY an_created DESC";
	// Perform Query	
	$result = $dbobj->MySQLDB_GetData($query,'ARRAY');	
	// Get num rows from the result set	
	$no_rows = $dbobj->MySQLDB_Numrows();				
	$ret['info_cnt'] = $no_rows;
	if($no_rows > 0)
	{
		$ret['info'] = $result;	
	}
 	display_view($ret, 'an_list_pg_visit');
}


/* ------------------------------------------------------------------------- */

/** 
 * Display View
 *
 * @desc this will set the view to inculde along with the header and footer
 * @access public
 * @param array	
 * @param string 
 * @return void
 */
function display_view($actions = array(),$view='')
{
	global $dbobj, $proj_config;
	$msg = 'Sorry, the view ("'.$view.'") doest not exists, please check and try again.';
	/*************************************************************/
	//Do not change this part
	include(BASEPATH . 'views/header.php');//include header file
	$fil_inc = BASEPATH . 'views/'.$view.'.php';
	if(is_file($fil_inc))
		include($fil_inc);//include body part
	else
		include(BASEPATH . 'views/404_view.php');//include 404 View page
	include(BASEPATH . 'views/footer.php');//include footer file
	/************************************************************/
}
?>